package com.zxx.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.zxx.ssm.dao.ApplicationClass;
import com.zxx.ssm.model.biz.StuAppBiz;
import com.zxx.ssm.model.form.StuAppForm;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.StuAppService;
import com.zxx.ssm.util.DateUtils;
import com.zxx.ssm.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("stuApp")
@Controller
public class StuAppController {
    @Autowired
    private StuAppService stuAppService;

    @RequestMapping("getAll")
    @ResponseBody
    public BizResponse getAll(@ModelAttribute StuAppForm stuAppForm, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                              @RequestParam(defaultValue = "id") String sort, @RequestParam(defaultValue = "desc") String order) {
        if("stuName".equals(sort))
            sort = "username";
        if("appTimeFormat".equals(sort))
            sort = "application_time";
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        stuAppForm.setTimeStart(DateUtils.convertString(stuAppForm.getTimeStart()));
        stuAppForm.setTimeEnd(DateUtils.convertString(stuAppForm.getTimeEnd()));
        List<StuAppBiz> stuAppBizList = stuAppService.getAll(stuAppForm);
        PageInfo pageInfo = new PageInfo(stuAppBizList);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuAppList", pageInfo);
        return bizResponse;
    }

    @RequestMapping(value = "updateStuApp", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse updateStuApp(@ModelAttribute ApplicationClass applicationClass) {
        ApplicationClass app = stuAppService.selectByPrimaryKey(applicationClass.getId());
        app.setResponse(applicationClass.getResponse());
        app.setState(applicationClass.getState());
        stuAppService.updateByPrimaryKey(app);
        return new BizResponse();
    }
}
